//
//  DTBluetoothDataHandle.m
//  BluetoothProject
//
//  Created by JianWei Chen on 17/2/7.
//  Copyright © 2017年 JianWei Chen. All rights reserved.
//

#import "DTCBluetoothDataHandle.h"
#import "DTCDeviceModel.h"
#import "DTCBluetoothConfig.h"
#import "DTCBlueToothSendData.h"

@implementation DTCBluetoothDataHandle
#pragma mark - 初始化
+ (DTCBluetoothDataHandle *)sharedInstance
{
    static DTCBluetoothDataHandle* static_manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        static_manager = [[DTCBluetoothDataHandle alloc] init];
        
    });
    return static_manager;
}

- (instancetype)init
{
    if ([super init]) {
        /*
         #define Notify_Characteristic_Back @"NotifyCharacteristicBack"//通知特征值返回值
         #define Notify_Characteristic_Read @"NotifyCharacteristicRead"//通知读取特征值
         #define Notify_Peripheral_Status @"NotifyPeripheralStatus"//通知外设状态
         #define Notify_Peripheral_Disconnect @"NotifyPeripheralDisconnect"//通知外设连接断开
         #define Notify_Peripheral_Connect @"NotifyPeripheralConnect"//通知外设连接
         */
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notifyCharacteristicBack:) name:Notify_Characteristic_Back object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notifyCharacteristicRead:) name:Notify_Characteristic_Read object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notifyPeripheralStatus:) name:Notify_Peripheral_Status object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notifyPeripheralDisconnect:) name:Notify_Peripheral_Disconnect object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notifyPeripheralConnect:) name:Notify_Peripheral_Connect object:nil];
    }
    return self;
}

#pragma mark - 通知处理

/**
 write特征值返回
 */
- (void)notifyCharacteristicBack:(NSNotification *)notify
{

}

/**
 read特征值返回
 */
- (void)notifyCharacteristicRead:(NSNotification *)notify
{

}

/**
 中心central状态
 */
- (void)notifyPeripheralStatus:(NSNotification *)notify
{
    
}

/**
 外设失去连接
 */
- (void)notifyPeripheralDisconnect:(NSNotification *)notify
{
    
}

/**
 外设连接成功
 */
- (void)notifyPeripheralConnect:(NSNotification *)notify
{
    DTCDeviceModel *device = notify.object;
    if (device && [device isKindOfClass:[DTCDeviceModel class]]) {
        [self sendData:device];
    }
}

#pragma mark - 事件处理
- (void)sendData:(DTCDeviceModel *)device
{
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        float percent = (arc4random() % 100)/100.f;
        float percent1 = (arc4random() % 100)/100.f;
        float percent2 = (arc4random() % 100)/100.f;
        
        NSString *string = [NSString stringWithFormat:@"c001%.2x%.2x%2.x000080800000000000000000000000",(int)(percent * 255),(int)(percent1 * 255),(int)(percent2 * 255)];
        NSData *data = [self DTconvertHexStrToData:string];
        [[DTCBlueToothSendData sharedInstance]sendDataByData:data andInfo:device];
    });
    dispatch_source_set_cancel_handler(timer, ^{
        NSLog(@"定时器停止了");
    });
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(50 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        dispatch_source_cancel(timer);
    });
    dispatch_resume(timer);
}

- (NSData *)DTconvertHexStrToData:(NSString *)str {
    if (!str || [str length] == 0) {
        return nil;
    }
    
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
    NSRange range;
    if ([str length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        
        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];
        
        range.location += range.length;
        range.length = 2;
    }
    
    return hexData;
}
@end
